use super::*;

/// A record that describes a range of glyph IDs.
#[derive(Clone, Copy)]
pub struct RangeRecord {
	/// First glyph ID in the range
	pub start: GlyphId,
	/// Last glyph ID in the range
	pub end: GlyphId,
	/// Coverage Index of first glyph ID in range.
	pub value: u16,
}
impl LazyArray16<'_, RangeRecord> {
	/// Returns a [`RangeRecord`] for a glyph.
	pub fn range(&self, glyph: GlyphId) -> Option<RangeRecord> {
		self.binary_search_by(|record| {
			if glyph < record.start {
				core::cmp::Ordering::Greater
			} else if glyph <= record.end {
				core::cmp::Ordering::Equal
			} else {
				core::cmp::Ordering::Less
			}
		})
		.map(|p| p.1)
	}
}
impl FromData for RangeRecord {
	const SIZE: usize = 6;
	#[inline]
	fn parse(data: &[u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		Some(RangeRecord {
			start: s.read::<GlyphId>()?,
			end: s.read::<GlyphId>()?,
			value: s.read::<u16>()?,
		})
	}
}
impl core::fmt::Debug for RangeRecord {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.write_fmt(format_args!("RangeRecord[{:5},{:5},{:5}]", self.start.0, self.end.0, self.value))
	}
}
